package com.shuqi.model.parse.parser;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import com.shuqi.database.model.BookCataLog;
import com.shuqi.database.model.BookInfo;
import com.shuqi.model.bean.BookData;
import com.shuqi.model.parse.HandlerBase;

/**
 * 一搜目录解析
 * 
 * @author zhangpeng
 * 
 */
public class YiSoDownCatalogParser extends HandlerBase {

    private List<BookCataLog> infos;
    private BookData data;
    private String mBookId;
    private String mSourceId;
    private String mUid;
    private int mOid;
    private int mMaxoid;

    /**
     * 构造函数
     * 
     * @param data2
     */
    public YiSoDownCatalogParser(String bookId,String sourceId,String uid) {
        mBookId = bookId;
        mSourceId = sourceId;
        mUid = uid;
        mOid = 1;
        mMaxoid = 0;
    }

    @Override
    public void startDocument() throws SAXException {
        data = new BookData();
        infos = data.getInfos();
        if (infos == null) {
            infos = new ArrayList<BookCataLog>();
            data.setInfos(infos);
        } else {
            infos.clear();
        }
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attrs)
            throws SAXException {
        if ("BookInfos".equals(localName)) {
            data.setChapterUpdateTime(System.currentTimeMillis());
            data.setBookId(isNull(attrs, "BookId"));
            data.setIsopen(isNull(attrs, "isopen"));
            data.setHide(isNull(attrs, "hide"));
            data.setSourceId(isNull(attrs, "src"));
            data.setBookName(isNull(attrs, "BookName"));
            data.setCoverImg(isNull(attrs, "imageUrl"));
            String totalCount = isNull(attrs, "TotalCount");
            if(totalCount != null){
                try{
                    mMaxoid = Integer.parseInt(totalCount);
                    data.setOidMax(mMaxoid);
                } catch(Exception e){
                } 
            }
            data.setAuthorName(isNull(attrs, "Author"));
            data.setWordCount(isNull(attrs, "wordCount"));
            data.setCoverIsopen(BookInfo.OPEN);
            data.setReadIsopen(BookInfo.OPEN);
            
        } else if ("Book".equals(localName)) {
            BookCataLog catalog = new BookCataLog();
            catalog.setBookId(mBookId);
            catalog.setSourceId(mSourceId);
            catalog.setUserId(mUid);
            infos.add(catalog);
            catalog.setChapterId(isNull(attrs, "ChapterId"));
            catalog.setChapterName(isNull(attrs, "BookChapter"));
            catalog.setChapterContentUrl(isNull(attrs, "osscontentURL"));
            catalog.setChapterState(BookCataLog.IS_CHATPTER);
            catalog.setPayState(BookCataLog.HAS_PAY);
            catalog.setPayMode(BookCataLog.BOOK_FREE);
            catalog.setChapterPrice("0");
            catalog.setChapterSourceUrl(isNull(attrs, "source_link"));
            
            if(catalog.getChapterState() == BookCataLog.IS_CHATPTER){
                catalog.setOId(mOid++);
                if(mOid-1>mMaxoid){
                    data.setOidMax(mOid-1);
                }
            }
        }
        super.startElement(uri, localName, qName, attrs);
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    @Override
    public BookData getParsedData() {
        return data;
    }

}
